package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.ShoppingDetailsActivity;
import com.wnhz.work.yz.bean.HomePageGridBean;
import com.wnhz.work.yz.bean.HomePageListBean;
import com.wnhz.work.yz.view.NoScrollGridView;

import org.xutils.x;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2016/12/6.
 */
public class HomePageListAdapter extends BaseAdapter {
    private Context context;
    private HomePageGridAdapter mAdapter;
    private List<HomePageListBean> pagelist;
    private List<HomePageGridBean> list = new ArrayList<>();
    private View.OnClickListener listener;

    public HomePageListAdapter(Context context, List<HomePageListBean> pagelist, View.OnClickListener listener) {
        this.context = context;
        this.pagelist = pagelist;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return pagelist.size();
    }

    @Override
    public Object getItem(int position) {
        return pagelist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        HomePageListViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new HomePageListViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.homepage_listview_item, null);
            viewHolder.iv_good = (ImageView) convertView.findViewById(R.id.iv_good);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            viewHolder.gv_sv = (NoScrollGridView) convertView.findViewById(R.id.gv_sv);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (HomePageListViewHolder) convertView.getTag();
        }
        x.image().bind(viewHolder.iv_good, pagelist.get(position).getIndex_pic());
        viewHolder.tv_name.setText(pagelist.get(position).getGoods_name());
        viewHolder.tv_content.setText(pagelist.get(position).getGoods_des());
        DecimalFormat df = new DecimalFormat("######0.00");
//        Log.e("----", MyApplication.getInstance().getUserBean().getRatio() + "");
        /*try {
            viewHolder.tv_price.setText("¥ " + df.format(Double.parseDouble(pagelist.get(position).getOprice()) * Double.parseDouble(MyApplication.getInstance().getUserBean().getRatio().toString())));
        } catch (Exception e) {*/
//            Log.e("----", Integer.parseInt(pagelist.get(position).getOprice())+"");

            viewHolder.tv_price.setText("¥ " + df.format(Double.parseDouble(pagelist.get(position).getOprice())));
//        }


//        HomePageGridBean bean = new HomePageGridBean();
//        for (int i = 0; i < imgs.length; i++) {
//            bean.setImg(imgs[i] + "");
//            list.add(bean);
//        }

//        mAdapter = new HomePageGridAdapter(context, list);
        mAdapter = new HomePageGridAdapter(context, pagelist.get(position).getSkus(), listener, position);
        mAdapter.setShow(pagelist.get(position).isShow());
        viewHolder.gv_sv.setAdapter(mAdapter);
        viewHolder.gv_sv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int minposition, long id) {
                Intent intent = new Intent(context, ShoppingDetailsActivity.class);
                //System.out.println( "onItemClick:goodsId====== "+ pagelist.get(position).getGoods_id());
                intent.putExtra("goodsId", pagelist.get(position).getGoods_id());
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    class HomePageListViewHolder {
        ImageView iv_good;
        TextView tv_name;
        TextView tv_content;
        TextView tv_price;
        NoScrollGridView gv_sv;
    }
}
